//package com.xiguaji.tiktok.task.aweme.event;
//
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.io.resource.ResourceUtil;
//import com.google.common.eventbus.AllowConcurrentEvents;
//import com.google.common.eventbus.Subscribe;
//import com.google.common.util.concurrent.AtomicDouble;
//import com.gugee.tiktok.common.model.dal.blogger.AwemeCommentRepository;
//import com.gugee.tiktok.common.model.dal.blogger.AwemeRepository;
//import com.gugee.tiktok.common.model.es.ESAweme;
//import com.gugee.tiktok.common.model.es.Keyword;
//import com.gugee.tiktok.common.model.mysql.Aweme;
//import com.gugee.tiktok.common.model.mysql.AwemeComment;
//import com.xiguaji.boot.eventbus.Event;
//import com.xiguaji.boot.eventbus.EventBusTemplate;
//import com.xiguaji.boot.eventbus.IEventListener;
//import com.xiguaji.boot.filecenter.s3.S3Template;
//import com.xiguaji.boot.toolkit.JsonUtil;
//import com.xiguaji.boot.toolkit.ThreadPoolUtils;
//import com.xiguaji.tiktok.task.aweme.client.BloggerClient;
//import com.xiguaji.tiktok.task.aweme.spider.AwemeSpiderClient;
//import com.xiguaji.tiktok.task.monitor.AwemesComentStatics;
//import com.xiguaji.tiktok.task.monitor.LogUtil;
//import com.xiguaji.tiktok.task.monitor.Monitor;
//import com.xxl.job.core.log.XxlJobLogger;
//import io.micrometer.core.instrument.search.Search;
//import lombok.Getter;
//import lombok.Setter;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import lombok.val;
//import org.apache.lucene.search.join.ScoreMode;
//import org.elasticsearch.action.bulk.BulkRequest;
//import org.elasticsearch.action.bulk.BulkResponse;
//import org.elasticsearch.action.get.GetRequest;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchRequestBuilder;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.action.update.UpdateRequest;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.common.io.stream.StreamInput;
//import org.elasticsearch.common.xcontent.XContentType;
//import org.elasticsearch.index.query.BoolQueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.elasticsearch.search.sort.SortOrder;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Lazy;
//import org.springframework.stereotype.Component;
//import org.springframework.web.client.ResourceAccessException;
//
//import javax.management.Query;
//import javax.management.QueryEval;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//import java.io.IOException;
//import java.time.LocalDateTime;
//import java.util.*;
//import java.util.concurrent.ForkJoinPool;
//
//@Slf4j
//@Component
//public class SaveAwemeKeywordEventListener implements IEventListener<SaveAwemeKeywordEventListener.SaveAwemeKeywordEvent> {
//    ForkJoinPool forkJoinPool = ThreadPoolUtils.forkJoinPool(20, "SaveAwemeKeyword-worker");
//    private final String AWEME_INDEX = "aweme_index_";
//    @Autowired
//    AwemeRepository awemeRepository;
//    @Autowired
//    RestHighLevelClient highLevelClient;
//    @Autowired
//    AwemeSpiderClient awemeSpiderClient;
//    @Autowired
//    AwemeCommentRepository awemeCommentRepository;
//    @Autowired
//    ESAweme.ESAwemeMapper esAwemeMapper;
//    @Autowired
//    BloggerClient bloggerClient;
//
//    @Lazy
//    @Autowired
//    EventBusTemplate eventBusTemplate;
//
//    @SneakyThrows
//    @Override
//    @Subscribe
//    @AllowConcurrentEvents
//    public void handle(SaveAwemeKeywordEvent event) {
//
//        if(null == event.aweme && null == event.awemeComments){
//            if(null == event.bloggerUid){
//                //补采关键词
//                run();
//            }else{
//                forkJoinPool.submit(() ->
//                        event.bloggerUid.parallelStream().forEach(bloggerId -> {
//                            try {
//                                runForUid(bloggerId);
//                            } catch (Exception ex) {
//                                log.error("UpdateBloggerMain error. bloggerId:" + bloggerId, ex.getMessage());
//                            }
//                        })
//                ).get();
//
//                // redis日志记录
//                LogUtil.delLogData();
//
//            }
//        }else{
//            //采集评论的任务发起
//            runByComment(event,event.awemeComments);
//        }
//
//    }
//
//
//
//    private void run(){
//        BulkRequest bulkRequest = new BulkRequest();
//        val esAwemes = getAwemeListByEsAndTagsIsNull();
//        esAwemes.forEach( a->{
//            val keywordList = getKeyword(a.getAwemeId());
//
//            if(keywordList.size() == 0){
//                Aweme aweme = new Aweme();
//                aweme.setId(a.getId());
//                aweme.setOAwemeId(a.getAwemeId());
//                aweme.setOCreateTime(a.getCreateTime());
//                aweme.setOCommentCount(a.getCommentCount());
//                val saveAwemeCommentEvent = new SaveAwemeCommentEventListener.SaveAwemeCommentEvent(new ArrayList<>(Arrays.asList(aweme)));
//                eventBusTemplate.post(saveAwemeCommentEvent);
//                return;
//            }
//            UpdateRequest updateRequest = new UpdateRequest();
//            updateRequest.index(AWEME_INDEX+getDateStr(a.getCreateTime()));
//            updateRequest.id(String.valueOf(a.getId()));
//            Map<String,List<Keyword>> map = new HashMap<>();
//            map.put("awemeTags",keywordList);
//            updateRequest.doc(JsonUtil.parseJson(map),XContentType.JSON);
//            bulkRequest.add(updateRequest);
//        });
//
//        if(bulkRequest.requests().size()>0){
//            BulkResponse bulk = null;
//            try {
//                bulk = highLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            //log.info("SaveAwemeKeywordEvent TagsIsNull keyword count:{}",esAwemes.size());
//        }
//    }
//
//
//    private void runByComment(SaveAwemeKeywordEvent event,List<AwemeComment> awemeComments){
//        Aweme aweme=event.aweme;
//
//        Map<Integer,String> requestBody = buildRequestBody(awemeComments);
//        if(requestBody.isEmpty()){
//            log.info("OAwemeId={},CommentsSize={}",aweme.getOAwemeId(),awemeComments.size());
//            return;
//        }
//        long start=System.currentTimeMillis();
//        //todo 提需求，让对方提供多视频粒度的接口，这样可批量解析，大大减少请求量
//        List<Keyword> keywordList = getKeywordByAi(requestBody,event);
//        event.awemeComentStatics.setGetKeywordByAiTime((System.currentTimeMillis()-start)/1000);
//        if(keywordList.size() == 0){
//            log.info("OAwemeId={},CommentsSize={},getKeywordsByAi=0",aweme.getOAwemeId(),awemeComments.size());
//            return;
//        }
//        start=System.currentTimeMillis();
//        UpdateRequest updateRequest = new UpdateRequest();
//        updateRequest.index(AWEME_INDEX+getDateStr(aweme.getOCreateTime()));
//        updateRequest.id(String.valueOf(aweme.getId()));
//        Map<String,List<Keyword>> map = new HashMap<>();
//        map.put("awemeTags",keywordList);
//        updateRequest.doc(JsonUtil.parseJson(map),XContentType.JSON);
//        try {
//            highLevelClient.update(updateRequest,RequestOptions.DEFAULT);
//        }catch (Exception e) {
//            XxlJobLogger.log("SaveAwemeKeywordEvent error ",e);
//            log.error("OAwemeId={},CommentsSize={},Exception={}",aweme.getOAwemeId(),awemeComments.size(),e.getMessage());
//        }finally {
//            event.awemeComentStatics.setSaveKeyword2EsTime((System.currentTimeMillis()-start)/1000);
//            event.awemeComentStatics.setKeywordSize(keywordList.size());
//        }
//
//
//    }
//
//
//    private void runForUid(long uid){
//        val awemeListByEs = getAwemeListByEs(uid);
//        awemeListByEs.removeIf( a -> {
//            if(null == a.getAwemeTags()){
//                return false;
//            }
//            if(a.getAwemeTags().size() == 0){
//                return false;
//            }
//            return true;
//        });
//        BulkRequest bulkRequest = new BulkRequest();
//        awemeListByEs.forEach( a->{
//            val keywordList = getKeyword(a.getAwemeId());
//
//            if(keywordList.size() == 0){
//                return;
//            }
//            UpdateRequest updateRequest = new UpdateRequest();
//            updateRequest.index(AWEME_INDEX+getDateStr(a.getCreateTime()));
//            updateRequest.id(String.valueOf(a.getId()));
//            Map<String,List<Keyword>> map = new HashMap<>();
//            map.put("awemeTags",keywordList);
//            updateRequest.doc(JsonUtil.parseJson(map),XContentType.JSON);
//            bulkRequest.add(updateRequest);
//        });
//
//        if(bulkRequest.requests().size()>0){
//            BulkResponse bulk = null;
//            try {
//                bulk = highLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            System.out.println("SaveAwemeKeywordEvent keyword bloggerId:"+uid+" count:"+awemeListByEs.size());
//            //log.info("SaveAwemeKeywordEvent keyword bloggerId:{} count:{}",uid,awemeListByEs.size());
//        }
//
//        // redis日志记录
//        synchronized (this){
//            LogUtil.addCurSize(1);
//        }
//    }
//
//    private String getDateStr(long second){
//        return DateUtil.format(DateUtil.calendar(second*1000).getTime(),"yyyyMM");
//    }
//
//
//    /**
//     * 从AI接口获取分词
//     * @param requestBody
//     * @return
//     */
//    private List<Keyword> getKeywordByAi(Map<Integer,String> requestBody,SaveAwemeKeywordEvent event ){
//        List<String> commentAnalyzed = null;
//        List<Keyword> keywords = new ArrayList<>();
//
//        try{
//
//            long start = System.currentTimeMillis();
//            commentAnalyzed = awemeSpiderClient.getCommentAnalyzed(requestBody);
//            if((System.currentTimeMillis()-start)/1000>5) {
//                log.info("SaveAwemeKeywordEvent CommentAnalyzed use time:" + (System.currentTimeMillis() - start) / 1000 + "s");
//                XxlJobLogger.log("SaveAwemeKeywordEvent CommentAnalyzed use time:" + (System.currentTimeMillis() - start) / 1000 + "s");
//            }
//
//        } catch (Exception e){
//            log.error("AwemeId={},SaveAwemeKeywordEvent CommentAnalyzed error! {} ",event.aweme.getOAwemeId(), e);
//            if(event.awemeComentStatics!=null) {
//                event.awemeComentStatics.getAwemesComentStatics().getTotalCommentAnalyzedErrors().incrementAndGet();
//            }
//        }
//
//        if(null == commentAnalyzed || commentAnalyzed.size() < 1 ){
//            return keywords;
//        }
//        Map<String,Integer> rels = new HashMap<>();
//
//        AtomicDouble keywordCount = new AtomicDouble();
//
//        for (String l : commentAnalyzed) {
//            if(null == rels.get(l)){
//                int count = Collections.frequency(commentAnalyzed,l);
//                rels.put(l,count);
//                keywordCount.addAndGet(count);
//            }
//        };
//
//        rels.forEach((k,v)->{
//            Keyword keyword = new Keyword();
//            keyword.setKeyword(k);
//            keyword.setPercent(((int)(v/keywordCount.get()*100*100))/100.0);
//            keywords.add(keyword);
//        });
//
//
//        if(keywords.size() > 1){
//            Collections.sort(keywords,(first,second)-> {
//                if(first.getPercent() > second.getPercent()){
//                    return -1;
//                } else if (first.getPercent() == second.getPercent()){
//                    return 0;
//                }
//                return 1;
//            });
//        }
//
//        return keywords.size() >10?keywords.subList(0,10):keywords;
//    }
//
//    private List<Keyword> getKeyword(long awemeId){
//        Map<Integer,String> requestBody = getRequestBody(awemeId);
//        List<Keyword> keywords = new ArrayList<>();
//
//        if(requestBody.isEmpty()){
//            return keywords;
//        }
//        return getKeywordByAi(requestBody,null);
//    }
//
//    private List<ESAweme> getAwemeListByEs(long uid){
//        SearchRequest searchRequest = new SearchRequest(getEsIndex());
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        searchSourceBuilder.query(QueryBuilders.matchQuery("uid",uid));
//        searchSourceBuilder.size(50);
//        searchRequest.source(searchSourceBuilder);
//        List<ESAweme> esAwemes = new ArrayList<>();
//        try {
//            SearchResponse searchResponse = highLevelClient.search(searchRequest,RequestOptions.DEFAULT);
//            val hits = searchResponse.getHits().getHits();
//
//            for (SearchHit hit : hits) {
//                val aweme = JsonUtil.jsonParse(ESAweme.class, hit.getSourceAsString());
//                esAwemes.add(aweme);
//            }
//
//        } catch (IOException e) {
//            log.error("SaveAwemeKeywordEvent error! bloggerId:",uid, e);
//        }
//        return esAwemes;
//    }
//
//    private List<ESAweme> getAwemeListByEsAndTagsIsNull() {
//        SearchRequest searchRequest = new SearchRequest(getEsIndex());
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
//        //boolQueryBuilder.minimumShouldMatch(3);
//        boolQueryBuilder.must(QueryBuilders.rangeQuery("commentCount").gt(50));
//
//
//
//        BoolQueryBuilder boolQueryBuilder2 = new BoolQueryBuilder();
//        boolQueryBuilder2.must(QueryBuilders.existsQuery("awemeTags"));
//
//        boolQueryBuilder.mustNot(QueryBuilders.nestedQuery("awemeTags",boolQueryBuilder2, ScoreMode.Total));
//        searchSourceBuilder.sort("createTime", SortOrder.DESC);
//        searchSourceBuilder.from(0);
//        searchSourceBuilder.size(500);
//        searchSourceBuilder.query(boolQueryBuilder);
//        searchRequest.source(searchSourceBuilder);
//        List<ESAweme> esAwemes = new ArrayList<>();
//        try {
//            SearchResponse searchResponse = highLevelClient.search(searchRequest,RequestOptions.DEFAULT);
//            val hits = searchResponse.getHits().getHits();
//
//            for (SearchHit hit : hits) {
//                val aweme = JsonUtil.jsonParse(ESAweme.class, hit.getSourceAsString());
//                esAwemes.add(aweme);
//            }
//
//        } catch (IOException e) {
//            log.error("SaveAwemeKeywordEvent TagsIsNull error! ", e.getMessage());
//        }
//        return esAwemes;
//
//    }
//
//    private String getEsIndex(){
//        LocalDateTime now = LocalDateTime.now();
//        StringBuffer stringBuffer = new StringBuffer(AWEME_INDEX+DateUtil.format(now,"yyyyMM")).append(",");
//        stringBuffer.append(AWEME_INDEX+DateUtil.format(now.minusMonths(1),"yyyyMM")).append(",");
//        stringBuffer.append(AWEME_INDEX+DateUtil.format(now.minusMonths(2),"yyyyMM"));
//        return stringBuffer.toString();
//    }
//
//
//    private String getCurMonthIndex(){
//        LocalDateTime now = LocalDateTime.now();
//        StringBuffer stringBuffer = new StringBuffer(AWEME_INDEX+DateUtil.format(now,"yyyyMM"));
//        return stringBuffer.toString();
//    }
//
//
//    private Map<Integer,String> getRequestBody(long awemeId){
//        long start = System.currentTimeMillis();
//        Map<Integer,String> body = new HashMap<>();
//
//        try{
//            String comment = awemeCommentRepository.get(awemeId);
//
//            if((System.currentTimeMillis()-start)/1000>5)
//                log.info("ESRefreshAwemeEventListener get s3 data awemeId:{} use time:{}s",awemeId,(System.currentTimeMillis()-start)/1000);
//            List<AwemeComment> comments = new ArrayList<>();
//            comments = JsonUtil.jsonParse(comments.getClass(),comment);
//
//            return buildRequestBody(comments);
//        }catch (Exception e){
//            log.error("ESRefreshAwemeEventListener get s3 data error!",e.getMessage());
//        }
//
//        return body;
//
//    }
//
//    private Map<Integer,String> buildRequestBody(List<AwemeComment> comments){
//        Map<Integer,String> body = new HashMap<>();
//
//        for (int i = 0; i < comments.size(); i++) {
//            body.put(i,comments.get(i).getText());
//        }
//
//        return body;
//    }
//
//
//@Setter
//@Getter
//    public static class SaveAwemeKeywordEvent extends Event {
//        public SaveAwemeKeywordEvent(List<Long> bloggerUid) {
//            super( null,"SaveAwemeKeywordEvent");
//            this.bloggerUid = bloggerUid;
//        }
//        public SaveAwemeKeywordEvent() {
//            super( "SaveAwemeKeywordEvent");
//        }
//
//
//        public SaveAwemeKeywordEvent(List<AwemeComment> awemeComments,Aweme aweme) {
//            super( "SaveAwemeKeywordEvent");
//            this.awemeComments = awemeComments;
//            this.aweme = aweme;
//        }
//
//        private List<Long> bloggerUid;
//
//        private List<AwemeComment> awemeComments;
//        private Aweme aweme;
//        private AwemesComentStatics.AwemeComentStatics awemeComentStatics;
//    }
//}
